<?php
/*
 * @Author: Wulala
 * @Date: 2025-03-25 15:21:48
 * @LastEditTime: 2025-04-01 19:33:27
 * @LastEditors: Wulala
 * @Description: 
 * @FilePath: \exchange\app\api\controller\third\Goods.php
 * 可以输入预定的版权声明、个性签名、空行等
 */

namespace app\api\controller\third;

use app\common\controller\ApiController;
use app\admin\model\MallGoods;
use think\exception\ValidateException;
use think\Request;

// 导入商品
class Goods extends ApiController
{
    public function addGoods(Request $request)
    {
        $li = $request->post();

        $params['cate_id']=$li['cate_id']??'';//分类ID
        $params['title']=$li['title']??'';//商品名称
        $params['logo']=$li['logo']??'';//商品logo
        $params['images']=$li['images']??'';//商品组图 以 | 做分割符号
        $params['describe']=$li['describe']??'';//商品描述
        $params['market_price']=$li['market_price']??'';//市场价
        $params['iscount_price']=$li['iscount_price']??'';//折扣价
        $params['sales']=$li['sales']??'';//销量
        $params['virtual_sales']=$li['virtual_sales']??'';//虚拟销量
        $params['stock']=$li['stock']??'';//剩余库存
        $params['total_stock']=$li['total_stock']??'';//总库存
        $params['sort']=$li['sort']??'';//排序
        $params['status']=$li['status']??'';//状态(1:禁用,2:启用)
        $params['remark']=$li['remark']??'';//备注说明


        try {
            validate([
                'cate_id'  => 'require',
                'title'   => 'require',
                'logo' => 'require',    
                'market_price' => 'require',    
                'iscount_price' => 'require',       
            ])->message([
                'cate_id.require'  => 'cate_id必须传递',
                'title.require'   => 'title必须传递',
                'logo.require' => 'logo必须传递',    
                'market_price.require' => 'market_price必须传递',    
                'iscount_price.require' => 'iscount_price必须传递',    
            ])->check($params);
        } catch (ValidateException $e) {
            // 验证失败 
            output_error($e->getError());
        }

        try {
            $result = (new MallGoods)->save($params);
            
            output_success('success');
        } catch (\Exception $e) {
            output_error('操作失败');
        }

    }
}